package com.yanqu.road.server.cmd.cookboy;

import com.yanqu.road.entity.activity.cookboy.CookBoyMatchDish;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchRank;
import com.yanqu.road.entity.activity.cookboy.CookBoyMatchReward;
import com.yanqu.road.entity.enums.eCookBoyFoodStatusType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.cookboy.CookBoyProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.cookboy.CrossCookBoyActivityMgr;
import com.yanqu.road.server.manager.cookboy.CrossCookBoyMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.C_CROSS_COOK_BOY_MATCH_REWARD, desc = "小当家第一阶段获取奖励信息")
public class CrossMatchRewardCmd extends CrossCommand {

    private long userId;
    private int activityId;
    private int no;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CookBoyProto.CookBoyMatchRewardReqMsg reqMsg = CookBoyProto.CookBoyMatchRewardReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        activityId = reqMsg.getActivityId();
        no = reqMsg.getNo();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CookBoyMatchReward cookBoyMatchReward = CrossCookBoyMgr.getMatchReward(activityId,userId,no);

        CookBoyProto.CookBoyMatchRewardRespMsg.Builder respMsg = CookBoyProto.CookBoyMatchRewardRespMsg.newBuilder();
        respMsg.setRet(cookBoyMatchReward.getRet());
        if(cookBoyMatchReward.getRet() == 0){
            respMsg.setNo(no);
            respMsg.setRank(cookBoyMatchReward.getRank());
            respMsg.setDishNum(cookBoyMatchReward.getCookBoyMatchDishList().size());
            respMsg.setReward(CrossCookBoyActivityMgr.COOKBOY_EXCHANGE_ITEM_ID + "=" + cookBoyMatchReward.getIntegral());
            respMsg.setTopScore(cookBoyMatchReward.getTopDish().getFraction());
            int rank = 1;
            for(CookBoyMatchRank cookBoyMatchRank : cookBoyMatchReward.getRankList()){
                CookBoyProto.CookBoyMatchRewardRankTempMsg.Builder tempUserMsg = CookBoyProto.CookBoyMatchRewardRankTempMsg.newBuilder();
                tempUserMsg.setUserId(cookBoyMatchRank.getUserId());
                tempUserMsg.setRank(rank);
                UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(cookBoyMatchRank.getUserId());
                if(userBaseInfo == null){
                    continue;
                }
                tempUserMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                for(CookBoyMatchDish cookBoyMatchDish : cookBoyMatchRank.getMatchDishList()){
                    CookBoyProto.CookBoyFinishFoodTempMsg.Builder foodMsg = CookBoyProto.CookBoyFinishFoodTempMsg.newBuilder();
                    foodMsg.setId(cookBoyMatchDish.getId());
                    foodMsg.setFoodId(cookBoyMatchDish.getFoodId());
                    foodMsg.setFraction(cookBoyMatchDish.getFraction());
                    foodMsg.setStatus(eCookBoyFoodStatusType.IsOver.getValue());
                    foodMsg.setNo(cookBoyMatchReward.getNo());
                    foodMsg.setRank(cookBoyMatchDish.getRank());
                    foodMsg.setTime(0);
                    tempUserMsg.addFoodList(foodMsg);
                }
                respMsg.addCookerList(tempUserMsg);
            }
        }
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_COOK_BOY_MATCH_REWARD, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {

    }

}
